package cn.t.core.util;

import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeansException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.stereotype.Component;

@Slf4j
@Component
public class SpringContextUtil implements ApplicationContextAware {

	private static final String APP_NAME = "spring.application.name";
	public static ApplicationContext applicationContext = null;


	@Override
	public void setApplicationContext(ApplicationContext context) throws BeansException {
		SpringContextUtil.applicationContext = context;
	}

	public static ApplicationContext getApplicationContext() {
		return applicationContext;
	}

	public static <T> T getBean(Class<T> clazz) {
		return getApplicationContext().getBean(clazz);
	}

	public static <T> T getBean(String className) {
		return (T) getApplicationContext().getBean(className);
	}

	public static <T> T getBean(String name, Class<T> clazz) {
		return getApplicationContext().getBean(name, clazz);
	}

	/**
	 * 返回当前应用名
	 */
	public static String getAppName() {
		return System.getProperty(APP_NAME);
	}

}
